<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<button onclick="goIndex()">返回</button>
<h1>学生列表页面</h1>

<table class="table table-hover">
    <thead>
    <tr>
        <th>账号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>爱好</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody  id="studentTableBody">

    </tbody>
</table>
<button onclick="loadStudents(1)">首页</button>
<button id="prevPage" style="display:none;" onclick="loadStudents(currentPage - 1)">上一页</button>
<button id="nextPage" style="display:none;" onclick="loadStudents(currentPage + 1)">下一页</button>
<button onclick="loadStudents(totalPage)">尾页</button>

<script src="js/jquery-1.8.2.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    let currentPage = 1;
    let totalPage;

    $(document).ready(function () {
        loadStudents(currentPage);

    });

    function loadStudents(curPage) {
        currentPage = curPage;
        $.ajax({
            url: "student?action=doGetStudents&curPage=" + curPage,
            method: 'GET',
            dataType: 'json',
            success: function (data) {
                totalPage=data.totalPage;//获取总页数
                let students = data.list;
                //获取学生列表
                let studentsStr = eval(students)
                // 清空表格内容
                $("#studentTableBody").empty();
                for (let i = 0;i<studentsStr.length;i++){
                    let username = studentsStr[i].student.username;
                    let name = studentsStr[i].student.name;
                    let sex = studentsStr[i].sex;
                    let age = studentsStr[i].student.age;
                    let hobbies = studentsStr[i].hobbies;

                    let tr = $("<tr></tr>");
                    let td1 = $("<td></td>").attr("id","username").text(username);
                    let td2 = $("<td></td>").text(name);
                    let td3 = $("<td></td>").text(sex);
                    let td4 = $("<td></td>").text(age);
                    let td5 = $("<td></td>").text(hobbies);
                    let td6 = $("<td></td>");
                    let a1 = $("<a></a>").attr("href","#").text("编辑");
                    a1.on("click", function() {
                        //跳转到学生初始化页面
                        window.location = "stuInfo2.jsp?username="+username;
                    });
                    // let a2 = $("<a></a>").attr("href","student?action=doDelete&username="+username).text("删除");
                    let a2 = $("<a></a>").attr("href","#").addClass("delete-link").data("username", username).text("删除");
                    $(td6).append(a1).append(" ").append(a2);
                    $(tr).append(td1).append(td2).append(td3).append(td4).append(td5).append(td6);
                    $("#studentTableBody").append(tr);
                }

                $(".delete-link").click(function (event) {
                        event.preventDefault(); // 阻止默认提交事件
                        let username = $(this).data("username");
                        if (confirm("确定要删除学生 " + username + " 吗？")) {
                            $.ajax(
                                {
                                    url: "student?action=delete&username=" + username,
                                    method: 'GET',
                                    dataType: 'json',
                                    success: function (data) {
                                        console.log(data);
                                        if (data == "1") {
                                            alert("删除成功");
                                            loadStudents(currentPage); // 重新加载当前页数据
                                        } else if (data == "0") {
                                            alert("删除失败");
                                        }
                                    }
                                });
                        }
                    }
                );
                console.log('AJAX请求成功:', students);
                updatePagination();
            },
            error: function (xhr, status, error) {
                console.log('AJAX请求失败:', error);
            }
        });
    }

    function updatePagination() {
        if (currentPage > 1) {
            $("#prevPage").show();
        } else {
            $("#prevPage").hide();
        }

        if (currentPage < totalPage) {
            $("#nextPage").show();
        } else {
            $("#nextPage").hide();
        }
    }
    function goIndex(){
        window.location = "index.html";
    }
</script>

</body>
</html>